//
//  MRNewNotificationViewController.m
//  MoRan
//
//  Created by MoRan on 2024/1/20.
//  Copyright © 2024 MoRan. All rights reserved.
//

#import "MRNewNotificationViewController.h"

#import "MRNewNotificationTextCell.h"
#import "MRNewNotificationContentTextCell.h"
#import "MRNewNotificationDateSelectCell.h"

#import <BRPickerView.h>

@interface MRNewNotificationViewController ()

@property (nonatomic,copy) NSString *mytitle;
@property (nonatomic,copy) NSString *subTitle;
@property (nonatomic,copy) NSString *content;
@property (nonatomic,copy) NSString *time;

@end

@implementation MRNewNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.backgroundColor = [UIColor clearColor];
    [self setNavigationItemTitleViewWithTitle:@"新建通知"];
    [self setRightItemWithIcon:[UIImage imageNamed:@"save_bt"] selector:@selector(saveButtonAction:)];

    [self.tableView registerNib:[UINib nibWithNibName:@"MRNewNotificationTextCell" bundle:nil] forCellReuseIdentifier:@"MRNewNotificationTextCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MRNewNotificationContentTextCell" bundle:nil] forCellReuseIdentifier:@"MRNewNotificationContentTextCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MRNewNotificationDateSelectCell" bundle:nil] forCellReuseIdentifier:@"MRNewNotificationDateSelectCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
                
        subarr = [NSMutableArray arrayWithCapacity:1];
        
        //标题
        dict =  @{@"class":MRNewNotificationTextCell.class,
                  @"height":@([MRNewNotificationTextCell getCellFrame:[NSNumber numberWithFloat:120]]),
                  @"data":[MRNewNotificationTextCellModel ittemModelWithTitle:@"标题" content:_mytitle],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        //副标题
        dict =  @{@"class":MRNewNotificationTextCell.class,
                  @"height":@([MRNewNotificationTextCell getCellFrame:[NSNumber numberWithFloat:120]]),
                  @"data":[MRNewNotificationTextCellModel ittemModelWithTitle:@"副标题" content:_subTitle],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        //内容
        dict =  @{@"class":MRNewNotificationContentTextCell.class,
                  @"height":@([MRNewNotificationContentTextCell getCellFrame:[NSNumber numberWithFloat:170]]),
                  @"data":[MRNewNotificationContentTextCellModel ittemModelWithTitle:@"内容" content:_content tip:@"请输入内容" max:@"500"],
                  @"delegate":@YES};
        [subarr addObject:dict];
    
        //推送时间
        dict =  @{@"class":MRNewNotificationDateSelectCell.class,
                  @"height":@([MRNewNotificationDateSelectCell getCellFrame:[NSNumber numberWithFloat:120]]),
                  @"data":[MRNewNotificationDateSelectCellModel ittemModelWithTitle:@"推送时间" content:_time],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark MRNewNotificationTextCellDelegate 标题 副标题

- (void)newNotificationTextCell:(MRNewNotificationTextCell *)cell textField:(UITextField *)textField {
    if ([cell.model.title isEqualToString:@"标题"]) {
        _mytitle = textField.text;
    } else if ([cell.model.title isEqualToString:@"副标题"]) {
        _subTitle = textField.text;
    }
}

#pragma mark MRNewNotificationContentTextCellDelegate 内容输入

- (void)newNotificationContentTextCell:(MRNewNotificationContentTextCell *)cell textView:(UITextView *)textView {
    _content = textView.text;
}

#pragma mark GPCurrencySelectCellDelegate 推送时间

- (void)newNotificationDateSelectCell:(MRNewNotificationDateSelectCell *)cell buttonAction:(UIButton *)sender {
    [self.view endEditing:YES];
    [self showDatePickerView];
}

#pragma mark 保存

- (void)saveButtonAction:(UIButton *)sender {

    NSString *erro = @"";
    
    if (_time.length == 0) {
        erro = @"请选择推送时间";
    }
    
    if (_content.length == 0) {
        erro = @"请输入内容";
    }
    
    if (_mytitle.length == 0) {
        erro = @"请输入标题";
    }
        
    if (erro.length > 0) {
        [self showProgressHUD:erro];
    } else {
        if (_completion) {
            MRNotificationModel *msg = [[MRNotificationModel alloc] init];
            msg.notificationId = [kDataManager getGuid];
            msg.title = _mytitle;
            msg.subTitle = _subTitle;
            msg.content = _content;
            msg.time = _time;
            _completion(self,msg);
        }
    }
}

- (void)showDatePickerView {
    // 1.创建日期选择器
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 320 - self.bottomBarHeight, kScreenWidth, 320 + self.bottomBarHeight)];
    datePickerView.pickerMode = BRDatePickerModeYMDHM;
    datePickerView.selectValue = _time;
    datePickerView.title = @"推送时间";
    datePickerView.minDate = [NSDate date];
    @weakify(self);
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        @strongify(self);
        NSLog(@"选择的值：%@", selectValue);
        self->_time = selectValue;
        [self refreshData];
    };
    // 设置自定义样式
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.alertViewColor = [UIColor whiteColor];
    customStyle.topCornerRadius = 20;
    customStyle.titleBarHeight = 56;
    customStyle.hiddenTitleLine = NO;
    
    /** 设置 picker 文本的颜色。暂不支持日期选择器前4种类型 */
    customStyle.pickerTextColor = UIColorFromHex(0x999999);

    /** 设置 picker 文本的字体。暂不支持日期选择器前4种类型 */
    customStyle.pickerTextFont = [UIFont boldSystemFontOfSize:16];

    /** 设置 picker 中间选中行文本的颜色。暂不支持日期选择器前4种类型 */
    customStyle.selectRowTextColor = UIColorFromHex(0x000000);

    /** 设置 picker 中间选中行文本的字体。暂不支持日期选择器前4种类型 */
    customStyle.selectRowTextFont = [UIFont boldSystemFontOfSize:16];;
    
    ////////////////////////// 标题栏中间label（titleLabel）///////////////////////////
    /** 设置 titleLabel 文本颜色 */
    customStyle.titleTextColor = UIColorFromHex(0x333333);
    /** 设置 titleLabel 字体大小 */
    customStyle.titleTextFont = [UIFont boldSystemFontOfSize:16];
    customStyle.titleLabelFrame = CGRectMake(kScreenWidth/2 - 200/2, 13, 200, 30);

    /////////////////////////////// 取消按钮（cancelBtn）//////////////////////////////

    /** 设置 cancelBtn 的背景颜色 */
    customStyle.cancelColor = [UIColor clearColor];
    
    customStyle.cancelBtnImage = [UIImage imageNamed:@"time_close"];

    /** 设置 cancelBtn 标题的字体 */
    customStyle.cancelTextFont = [UIFont boldSystemFontOfSize:14];

    /** 设置 cancelBtn 的 frame */
    customStyle.cancelBtnFrame = CGRectMake(15, 13, 70, 30);

    /** 设置 cancelBtn 的边框样式 */
    customStyle.cancelBorderStyle = BRBorderStyleFill;

    /** 设置 cancelBtn 的圆角大小 */
    customStyle.cancelCornerRadius = 15;

    /** 设置 cancelBtn 的边框宽度 */
    //@property (nonatomic, assign) CGFloat cancelBorderWidth;

    /** 设置 cancelBtn 的 image */
    //@property (nullable, nonatomic, strong) UIImage *cancelBtnImage;
    
    customStyle.separatorColor = UIColorFromHexA(0xABE56F,0.45);
    customStyle.separatorHeight = 0;

    /////////////////////////////// 确定按钮（doneBtn）////////////////////////////////

    /** 设置 doneBtn 的背景颜色 */
    customStyle.doneColor = [UIColor clearColor];

    /** 设置 doneBtn 标题的字体 */
    customStyle.doneTextFont = [UIFont boldSystemFontOfSize:14];

    /** 设置 doneBtn 的 frame */
    customStyle.doneBtnFrame = CGRectMake(kScreenWidth - 70 - 15, 13, 70, 30);

    /** 设置 doneBtn 的边框样式 */
    customStyle.doneBorderStyle = BRBorderStyleFill;

    /** 设置 doneBtn 的圆角大小 */
    customStyle.doneCornerRadius = 15;

    /** 设置 doneBtn 的边框宽度 */
    //@property (nonatomic, assign) CGFloat doneBorderWidth;

    /** 设置 doneBtn 的 image */
    //@property (nullable, nonatomic, strong) UIImage *doneBtnImage;
    
    customStyle.doneBtnImage = [UIImage imageNamed:@"ywc_select"];

    datePickerView.pickerStyle = customStyle;

    // 3.显示
    [datePickerView show];
}

#pragma mark - NSNotification

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification {
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;   //height 就是键盘的高度
    self.tableView.frame = CGRectMake(0, self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight - height);
}
 
//当键盘退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {
    self.tableView.frame = CGRectMake(0, self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
